package primetest.impl.dao;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.sql.Date;
import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.NewsDto;
import primetest.impl.dao.NewsDao;
import primetest.impl.dao.Transaction;

//@Stateless
public class NewsDao implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 6551770662358620809L;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private static List<NewsDto> allItems = null;
	private static HashMap<String, NewsDto> map = null;

	@Inject
	public NewsDao(){
	}

	public List<NewsDto> getAllList(Transaction transaction) throws IOException {
		if (allItems != null) {
			return allItems;
		}
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		map = new HashMap<String, NewsDto>();
		try {

			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(GET_ALL);
			resultSet = prepareStatement.executeQuery();

			while (resultSet.next()) {
				NewsDto newsDto = makeNewsDto(transaction, resultSet);
				map.put(newsDto.getKey(), newsDto);
			}
			allItems = new ArrayList<NewsDto> (map.values());
			return allItems;
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	private NewsDto makeNewsDto(Transaction transaction, ResultSet resultSet) throws SQLException, IOException {
		NewsDto newsDto = new NewsDto(resultSet.getString(1), resultSet.getString(2),
				resultSet.getString(3), resultSet.getDate(4));
		return newsDto;
	}


	public void addNews(Transaction transaction, NewsDto newsDto) throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(INSERT_ONE);
			prepareStatement.setString(1, newsDto.getKey());
			prepareStatement.setString(2, newsDto.getTitle());
			prepareStatement.setString(3, newsDto.getContent());
			prepareStatement.setDate(4, new Date(newsDto.getDate().getTime()));
			prepareStatement.executeUpdate();
			map.put(newsDto.getKey(), newsDto);
			allItems.add(newsDto);
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}
	}

	public void updateNews(Transaction transaction, NewsDto newsDto) throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(UPDATE_ONE);
			prepareStatement.setString(4, newsDto.getKey());
			prepareStatement.setString(1, newsDto.getTitle());
			prepareStatement.setString(2, newsDto.getContent());
			prepareStatement.setDate(3, new Date(newsDto.getDate().getTime()));
			prepareStatement.executeUpdate();
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	public void deleteNews(Transaction transaction, String newsSeq) throws IOException {
		PreparedStatement prepareStatement = null;
		ResultSet resultSet = null;
		try {
			Connection connection = transaction.getResource(Connection.class);
			prepareStatement = connection.prepareStatement(DELETE_ONE);
			prepareStatement.setString(1, newsSeq);
			prepareStatement.executeUpdate();
			allItems.remove(map.remove(newsSeq));
		} catch (SQLException e) {
			throw new IOException(e);
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}

			if (prepareStatement != null) {
				try {
					prepareStatement.close();
				} catch (SQLException e) {
					logger.warn(e.getMessage(), e);
				}
			}
		}

	}

	private static final String GET_ALL = "select key_1, title, content, date from s_news order by date";
	private static final String INSERT_ONE = "insert into s_news(key_1, title, content, date) values (?, ?, ?, ?)";
	private static final String UPDATE_ONE = "update s_news set title = ?, content = ?,  date = ? where key_1 = ?";
	private static final String DELETE_ONE = "delete from s_news where key_1 = ?";
}
